<template>
  <section>
    <div class="filter-container">
      <common-input @keyup.enter.native="handleFilter" class="w200" placeholder="小店名称" v-model="listQuery.keyword" />
      <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="用户"
        v-model="listQuery.memberKeyword" />
      <!-- <el-date-picker
            class="ml10"
            size="mini"
            v-model="listQuery.contractEnd"
            type="date"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
            placeholder="注册时间">
          </el-date-picker> -->
      <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="mini" class="ml10" @click="addAccount" type="primary" icon="el-icon-plus">新增</el-button>
    </div>
    <!-- <el-tabs size="mini" :before-leave='leaveTab' v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="0"></el-tab-pane>
        <el-tab-pane label="已审核" name="1"></el-tab-pane>
        <el-tab-pane label="待审核" name="2"></el-tab-pane>
        <el-tab-pane label="被驳回" name="3"></el-tab-pane>
      </el-tabs> -->
    <el-table max-height="500" :data="list" style="width: 100%">
      <el-table-column align="left" label="企业店名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="超管姓名" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.superName }}（{{ scope.row.superPhone }}）</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="地区" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.provinceText }}{{ scope.row.cityText }}{{ scope.row.countyText }}</span>
        </template>
      </el-table-column>
      <el-table-column align="right" label="已建店铺数">
        <template slot-scope="scope">
          <span>{{ scope.row.shopNum }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="缴费金额">
        <template slot-scope="scope">
          <span>{{ scope.row.payMoney }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="left" label="地址">
                  <template slot-scope="scope">
                      <span>{{scope.row.address}}</span>
                  </template>
              </el-table-column> -->
      <el-table-column align="center" label="添加时间" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <el-tag :type="getStatusColor(scope.row)">{{ scope.row.status ? '已禁止' : '已启用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item @click.native="openShopDetail(scope.row.id)">详情</el-dropdown-item> -->
              <el-dropdown-item @click.native="editDialog(scope.row.id)">编辑</el-dropdown-item>
              <!-- <el-dropdown-item v-if="scope.row.status==0" @click.native="handleCheck(scope.row)">审核</el-dropdown-item> -->
              <!-- <el-dropdown-item v-if="scope.row.status==1" @click.native="handleCheck2(scope.row)">取消审核</el-dropdown-item> -->
              <!-- <el-dropdown-item v-if="scope.row.status==0&&scope.row.isFront==1" @click.native="handleRefuse(scope.row)">拒绝审核</el-dropdown-item> -->
              <!-- <el-dropdown-item @click.native="handleRecharge(scope.row)">充值</el-dropdown-item> -->
              <el-dropdown-item @click.native="handleDelete(scope.row.id)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[20, 30, 50]" :page-size="listQuery.num"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="60%" :close-on-click-modal="false"
      @closed="cancel">
      <el-form :model="formDataAdd" :rules="rulesAdd" ref="formAdd" label-width="130px" class="formAdd">
        <el-form-item label="企业店名称" prop="name" class="formItem">
          <el-input v-model="formDataAdd.name" placeholder="请输入企业店名称" type="text"></el-input>
        </el-form-item>
        <!-- <el-form-item label="店铺类型" prop="shopType" class="formItem">
              <el-input v-model="formDataAdd.shopType" placeholder="请输入店铺类型" type="text"></el-input>
            </el-form-item> -->
        <el-form-item label="区域" prop="icon" class="formItem">
          <el-cascader placeholder="请选择地区" size="mini" :options="options" v-model="formDataAdd.province"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="建店铺数" prop="shopNum" class="formItem">
          <el-input v-model="formDataAdd.shopNum" placeholder="请输入已建店铺数" type="number"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile" class="formItem">
          <el-input v-model="formDataAdd.mobile" placeholder="请输入手机号" type="text"></el-input>
        </el-form-item>
        <el-form-item label="邀请主账号" prop="hostAccount" class="formItem">
          <el-input v-model="formDataAdd.hostAccount" placeholder="请输入邀请主账号" type="text"></el-input>
        </el-form-item>
        <el-form-item label="超管姓名" prop="superName" class="formItem">
          <el-input v-model="formDataAdd.superName" placeholder="请输入邀请主账号" type="text"></el-input>
        </el-form-item>
        <el-form-item label="超管手机号" prop="superPhone" class="formItem">
          <el-input v-model="formDataAdd.superPhone" placeholder="请输入超管手机号" type="text"></el-input>
        </el-form-item>
        <el-form-item label="缴费金额" prop="payMoney" class="formItem">
          <el-input v-model="formDataAdd.payMoney" placeholder="请输入缴费金额" type="text"></el-input>
        </el-form-item>


        <el-form-item label="状态" prop="status" class="formItem">

          <el-radio-group v-model="formDataAdd.status" v-removeAriaHidden>
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="合伙人ID" prop="partnerId">
              <el-input v-model="formDataAdd.partnerId" placeholder="请输入合作人ID" type="text"></el-input>
            </el-form-item>
            <el-form-item label="小店ID" prop="sellerId">
              <el-input v-model="formDataAdd.sellerId" placeholder="请输入小店ID" type="text"></el-input>
            </el-form-item> -->
        <el-form-item label="备注" prop="remark" class="formItem">
          <el-input v-model="formDataAdd.remark" placeholder="请输入备注" type="text"></el-input>
        </el-form-item>



        <el-form-item style="display: flex; justify-content: flex-end; width: 100%">
          <el-button type="primary" @click="submitAdd">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </section>
</template>

<script>
import { useCascaderAreaData } from '@vant/area-data';
import {
  settlementEntity, settlementEntityUpdate, settlementEntityAdd, settlementEntityDetail, settlementEntityDelete
} from '../../api/store';
export default {
  components: {
    //   'pic-sel': () => import('../../components/PicSel/index')
  },
  data() {
    return {
      regionData: [],
      checkLoading: false,
      dialogTitle: '新增',
      checkForm: {
        status: 1,
        note: undefined,
        id: 0,
      },
      dialogCheckFormVisible: false,
      dialogFormVisible: false,
      rechargeLoading: false,
      list: null,
      listLoading: true,
      options: useCascaderAreaData(),
      equipmentdistrict: "",
      //   arealist{},
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        //   keyword: '',
        //   partnerId: 0,
        //   sellerId: 0,
        //   superPhone:'',
        //   shopType:'',
        //   name:'',
        //   province:'',
        //   city:'',
        //   county:'',
        //   hostAccount:'',
        //   startAt:'',
        //   endAt:'',
        //   search:''
      },
      rulesAdd: {
        name: [
          { required: true, message: '用户名必填', trigger: 'blur' },
        ],
        superName: [
          { required: true, message: '超管姓名必填', trigger: 'change' },
        ],
        superPhone: [
          { required: true, message: '超管手机号必填', trigger: 'change' },
        ],
      },
      formDataAdd: {
        id: '',
        name: '',
        shopType: '',
        province: '',
        city: '',
        county: '',
        shopNum: 0,
        hostAccount: 1,
        superName: '',   //类型，1：平台用户，2：合伙人用户，3小店用户
        superPhone: '',
        payMoney: 0,
        status: 0,
        remark: '',

      },
      total: null,
      activeName: '0',
      rechargeForm: {
        sellerId: 0,
        money: undefined,
        note: undefined,
      }
    };
  },
  mounted() {
    this.regionData = useCascaderAreaData()
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.listLoading = true;
      settlementEntity(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    handleChange(value) {
      //   console.log('value', value)
      this.formDataAdd.city = value[1]
      this.formDataAdd.province = value[0]
      this.formDataAdd.county = value[2]
    },
    // 取消
    cancel() {
      this.dialogVisible = false
      this.dialogFormVisible = false
      // this.$refs['form'].clearValidate()
      //   this.formData = {
      //     id: 0,
      //     username: '',
      //     oldPassword: '',
      //     password: '',
      //     confirmPassword: ''
      //   }
    },
    // 新增
    submitAdd() {

      this.$refs['formAdd'].validate(valid => {
        console.log(valid, 'valid')
        if (valid) {

          if (this.formDataAdd.serviceManager) {
            if (!this.formDataAdd.memberId) {
              this.$message.error('请输入绑定会员id')
              return false
            }
            // this.formDataAdd.memberId ? '' : this.$message.error('请输入绑定会员id')

          }
          // console.log(this.formDataAdd)
          if (this.formDataAdd.id) {
            settlementEntityUpdate(this.formDataAdd).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message.success('修改成功')
                this.getList();
                this.dialogFormVisible = false
              } else {
                this.$message.error(res.message)
              }
            })
          } else {

            settlementEntityAdd(this.formDataAdd).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message.success('创建成功')
                this.getList();
                this.dialogFormVisible = false
              } else {
                this.$message.error(res.message)
              }
            })
          }
        }
      })
    },
    // 删除
    handleDelete(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        settlementEntityDelete(id).then(res => {
          if (res.code === 200) {
            this.getList();
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
        })
      })

    },
    // 修改信息弹窗
    editDialog(id) {
      this.dialogTitle = "修改"
      let sid = id
      //   this.getRoleList()
      // this.getRole(id)
      if (id) {
        settlementEntityDetail(sid).then(res => {
          console.log(res)
          if (res.code === 200) {
            // this.$message.success('修改成功')
            this.formDataAdd = res.data

            // this.formDataAdd.type === 1 ? this.formDataAdd.typeText = "平台用户" : this.formDataAdd.type === 2 ? this.formDataAdd.typeText = "合伙人用户" : this.formDataAdd.typeText = '小店用户'
            // this.arealist.push(res.data.p)

            this.dialogFormVisible = true
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },

    // 新增按钮
    addAccount() {
      console.log(this.formDataAdd)
      this.formDataAdd = {
        id: '',
        name: '',
        shopType: '',
        province: '',
        city: '',
        county: '',
        shopNum: 0,
        hostAccount: 1,
        superName: '',   //类型，1：平台用户，2：合伙人用户，3小店用户
        superPhone: '',
        payMoney: 0,
        status: 0,
        remark: '',
      }
      this.dialogTitle = '新增管理员'
      this.dialogFormVisible = true
    },
    //打开小店详情
    openShopDetail(id) {
      //   router.push({path:'/'})
      //跳转小店详情
      // store.dispatch("setDetail", data);
      this.$router.push({ path: "./shoplistDetail", query: { shopid: id } });
    },
    handleFilter() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleClick(tab, event) {
      this.listQuery.status = this.activeName;
      this.handleFilter();
      // console.log(this.activeName)
    },
    leaveTab(activeName, oldActiveName) {
      // console.log(activeName)
      // return false;
    },
    handleCreate() {
      this.$router.push("/company/shop_create")
    },
    handleUpdate(row) {
      this.$router.push("/company/shop_edit?id=" + row.id)
    },
    getStatusColor(row) {
      if (row.status === 1) {
        return "success"
      }

      return "danger"
    },
    handleDeletesss(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seller_del(row.id)
            .then(() => {
              this.$message.success("小店删除成功");
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        }).catch(() => { });
    },
    handleCheck(row) {
      this.checkForm.id = row.id;
      this.checkForm.status = 1;
      this.checkForm.note = "";

      this.dialogCheckFormVisible = true;
    },
    handleCheck2(row) {
      this.$confirm('确定取消审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seller_check2(row.id)
            .then(() => {
              this.$message.success("小店取消审核成功");
              this.getList();
            });
        }).catch(() => { });
    },
    handleRefuse(row) {
      this.$confirm('确定拒绝审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seller_refuse(row.id)
            .then(() => {
              this.$message.success("小店拒绝审核成功");
              this.getList();
            });
        }).catch(() => { });
    },
    handleRecharge(row) {
      this.rechargeForm.sellerId = row.id;
      this.rechargeForm.money = undefined;
      this.rechargeForm.note = undefined;

      this.dialogFormVisible = true;
    },
    okRecharge() {
      if (typeof this.rechargeForm.money == undefined) {
        this.$message.error("请输入充值金额");
      }

      let money = $.trim(this.rechargeForm.money);
      if (money.length < 1) {
        this.$message.error("请输入充值金额");
      } else {
        money = Number(money) || 0;

        if (money == 0) {
          this.$message.error("请输入充值金额");
        } else {
          this.rechargeLoading = true;
          seller_recharge(this.rechargeForm).then((re) => {
            this.rechargeLoading = false;
            this.$message.success("成功充值");
            this.dialogFormVisible = false;
            this.getList();
          }).catch((err) => {
            this.rechargeLoading = false;
          })
        }
      }
    },
    check(formName) {
      this.checkForm.note = $.trim(this.checkForm.note)

      // console.log(this.form)
      if (this.checkForm.status == 2) {
        if (this.checkForm.note.length < 1) {
          this.$message.error("拒绝审核时备注必填");
          return;
        }
      }
      this.checkLoading = true;
      seller_check(this.checkForm).then((res) => {
        this.checkLoading = false;
        this.$message.success("小店已审核");
        this.dialogCheckFormVisible = false;
        this.getList();
      }).catch((err) => {
        this.checkLoading = false;

      })
    }
  }
};
</script>
